<template>
  <div>
    <!-- 面包屑导航区 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>师生管理</el-breadcrumb-item>
      <el-breadcrumb-item>教师管理</el-breadcrumb-item>
    </el-breadcrumb>

    <hr />

    <!-- 卡片视图区 -->
    <el-card>
      <!-- 搜索区域 -->
      <el-row :gutter="20">
        <el-col :span="7">
          <el-input
            placeholder="请输入身份证"
            v-model="queryInfo.tecPerId"
            clearable
          >
            <template slot="prepend">身份证</template>
          </el-input>
        </el-col>

        <el-col :span="7">
          <el-input
            placeholder="请输入教师姓名"
            v-model="queryInfo.tecName"
            clearable
          >
            <template slot="prepend">姓名</template>
          </el-input>
        </el-col>

        <el-col :span="3">
          <el-button type="primary" @click="getTecList">查询</el-button>
        </el-col>
      </el-row>

      <!-- 功能区域 -->
      <el-row :gutter="10">
        <el-col :span="6">
          <el-button type="primary" @click="showAddDialog">添加教师</el-button>
        </el-col>
      </el-row>

      <!-- 用户列表区域 -->
      <el-table :data="tecList" border stripe style="width: 100%">
        <el-table-column
          prop="tecId"
          label="教师编号"
          width="100px"
        ></el-table-column>
        <el-table-column prop="tecName" label="姓名" sortable></el-table-column>
        <el-table-column prop="phone" label="手机号"></el-table-column>
        <el-table-column prop="email" label="电子邮箱"></el-table-column>
        <el-table-column prop="status" label="状态" align="center">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.status == '1'">正常</el-tag>
            <el-tag type="info" v-else-if="scope.row.status == '0'"
              >请假</el-tag
            >
            <el-tag type="danger" v-else-if="scope.row.status == '2'"
              >离岗</el-tag
            >
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <!-- 修改 -->
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              plain
              @click="showEditDialog(scope.row.tecId)"
              >编辑</el-button
            >

            <!-- 删除按钮 -->
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              plain
              @click="delDialog(scope.row.tecId)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页区域 -->
      <el-pagination
        @current-change="handleCurrentChange"
        :current-page.sync="queryInfo.page"
        :page-size="queryInfo.size"
        layout="total, prev, pager, next"
        :total="totalPage"
        background
      >
      </el-pagination>
    </el-card>

    <!-- 添加教师 -->
    <el-dialog
      title="添加教师"
      :visible.sync="addDialogVisible"
      width="width"
      @close="addDialogClosed"
    >
      <el-form
        ref="addRef"
        :model="addForm"
        :rules="addRules"
        label-width="100px"
      >
        <el-form-item label="所属分院" prop="deptId">
          <el-select v-model="addForm.deptId" placeholder="请选择分院">
            <el-option
              v-for="item in deptList"
              :key="item.deptId"
              :label="item.deptName"
              :value="item.deptId"
            >
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="教师姓名" prop="tecName">
          <el-input v-model="addForm.tecName"></el-input>
        </el-form-item>

        <el-form-item label="教师身份证" prop="tecPerId">
          <el-input v-model="addForm.tecPerId"></el-input>
        </el-form-item>

        <el-form-item label="性别" prop="sex">
          <el-radio-group v-model="addForm.sex">
            <el-radio :label="1">男</el-radio>
            <el-radio :label="0">女</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item label="手机号" prop="phone">
          <el-input v-model="addForm.phone"></el-input>
        </el-form-item>

        <el-form-item label="电子邮件" prop="email">
          <el-input v-model="addForm.email"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addSub">确 定</el-button>
      </div>
    </el-dialog>

    <!-- 编辑教师 -->
    <el-dialog
      title="编辑教师"
      :visible.sync="editDialogVisible"
      width="width"
      @close="editDialogClosed"
    >
      <el-form
        ref="editRef"
        :model="editForm"
        :rules="editRules"
        label-width="100px"
      >
        <el-form-item label="所属分院" prop="deptId">
          <el-select v-model="editForm.deptId" placeholder="请选择分院">
            <el-option
              v-for="item in deptList"
              :key="item.deptId"
              :label="item.deptName"
              :value="item.deptId"
            >
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="教师姓名" prop="tecName">
          <el-input v-model="editForm.tecName" disabled></el-input>
        </el-form-item>

        <el-form-item label="教师身份证" prop="tecPerId">
          <el-input v-model="editForm.tecPerId" disabled></el-input>
        </el-form-item>

        <el-form-item label="性别" prop="sex">
          <el-radio-group v-model="editForm.sex">
            <el-radio label="1" disabled>男</el-radio>
            <el-radio label="0" disabled>女</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item label="手机号" prop="phone">
          <el-input v-model="editForm.phone"></el-input>
        </el-form-item>

        <el-form-item label="电子邮件" prop="email">
          <el-input v-model="editForm.email"></el-input>
        </el-form-item>

        <el-form-item label="当前状态" prop="status">
          <el-select v-model="editForm.status">
            <el-option label="正常" value="1"></el-option>
            <el-option label="请假" value="0"></el-option>
            <el-option label="离岗" value="2"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editSub">确 定</el-button>
      </div>
    </el-dialog>

    <!-- 删除警告框 -->
    <el-dialog title="警告" :visible.sync="delDialogVisible" width="30%">
      <div>您确定要删除此教师吗？</div>
      <div style="color: red;">删除此教师可能会影响部门表，请谨慎操作。</div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="delDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="delTecById">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    // 验证邮箱
    var checkEmail = (rule, value, cb) => {
      const regEmail = /^[A-Za-z\d]+([-_.][A-Za-z\d]+)*@([A-Za-z\d]+[-.])+[A-Za-z\d]{2,4}$/
      if (regEmail.test(value)) {
        return cb()
      }
      cb(new Error('邮箱格式有误'))
    }

    // 验证手机号
    var checkMobile = (rule, value, cb) => {
      const regMobile = /^[1][3,4,5,7,8][0-9]{9}$/
      if (regMobile.test(value)) {
        return cb()
      }
      cb(new Error('手机号输入错误'))
    }

    // 验证身份证号
    var checkPerId = (rule, value, cb) => {
      // const reg = /^[1-9]\d{5}(18|19|20|(3\d))\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/
      const reg = /^[0-9]{18}$/
      if (reg.test(value)) {
        return cb()
      }
      cb(new Error('身份证号输入有误'))
    }
    return {
      tecList: [],
      deptList: [],
      queryInfo: {
        page: 1,
        size: 10,
        tecPerId: '',
        tecName: ''
      },
      totalPage: 0,
      addDialogVisible: false,
      addForm: {
        tecName: '',
        deptId: '',
        tecPerId: '',
        sex: '',
        phone: '',
        email: '',
        createBy: ''
      },
      addRules: {
        deptId: [{ required: true, message: '请选择分院', trigger: 'blur' }],
        tecName: [{ required: true, message: '请输入姓名', trigger: 'blur' }],
        tecPerId: [
          { required: true, message: '请输入身份证号', trigger: 'blur' },
          {
            validator: checkPerId,
            trigger: 'blur'
          }
        ],
        sex: [{ required: true, message: '请选择性别', trigger: 'blur' }],
        phone: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          {
            validator: checkMobile,
            trigger: 'blur'
          }
        ],
        email: [
          { required: true, message: '请输入电子邮件', trigger: 'blur' },
          {
            validator: checkEmail,
            trigger: 'blur'
          }
        ]
      },
      editDialogVisible: false,
      editForm: {
        tecId: '',
        tecName: '',
        deptId: '',
        tecPerId: '',
        sex: '',
        phone: '',
        email: '',
        updateBy: ''
      },
      editRules: {
        deptId: [{ required: true, message: '请选择分院', trigger: 'blur' }],
        phone: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          {
            validator: checkMobile,
            trigger: 'blur'
          }
        ],
        email: [
          { required: true, message: '请输入电子邮件', trigger: 'blur' },
          {
            validator: checkEmail,
            trigger: 'blur'
          }
        ]
      },
      delTecId: 0,
      delDialogVisible: false
    }
  },
  created() {
    this.getTecList()
  },
  methods: {
    async getTecList() {
      const { data: res } = await this.$http.get('tec/list', {
        params: this.queryInfo
      })
      if (res.code !== 20000) {
        return this.$message.error('获取列表信息失败')
      }
      this.tecList = res.data.list
      this.totalPage = res.data.total
    },
    handleCurrentChange(newPage) {
      this.queryInfo.page = newPage
      this.getTecList()
    },
    showAddDialog() {
      this.addDialogVisible = true
    },
    async getAllDept() {
      const { data: res } = await this.$http.get('dept/allDept')
      if (res.code !== 20000) {
        return this.$message.error('分院列表获取失败!')
      }
      this.deptList = res.data
    },
    addSub() {
      this.$refs.addRef.validate(async valid => {
        if (!valid) return
        this.addForm.createBy = window.sessionStorage.getItem('rootname')
        const { data: res } = await this.$http.post('tec/add', this.addForm)
        if (res.code === 20005) {
          return this.$message.error('添加教师失败，对象已存在!')
        } else if (res.code !== 20000) {
          return this.$message.error('添加教师失败，其他错误!')
        }
        this.addDialogVisible = false
        this.getTecList()
        return this.$message.success('添加教师信息成功!')
      })
    },
    addDialogClosed() {
      this.$refs.addRef.resetFields()
    },
    async showEditDialog(id) {
      const { data: res } = await this.$http.get('tec/one/' + id)
      if (res.code !== 20000) {
        return this.$message.error('查询教师个人信息失败!')
      }
      this.editForm = res.data
      this.editDialogVisible = true
    },
    editSub() {
      this.$refs.editRef.validate(async valid => {
        if (!valid) return
        this.editForm.updateBy = window.sessionStorage.getItem('rootname')
        const { data: res } = await this.$http.put('tec/update', this.editForm)
        if (res.code !== 20000) {
          return this.$message.error('更新失败!')
        }
        this.getTecList()
        this.editDialogVisible = false
        return this.$message.success('更新成功!')
      })
    },
    editDialogClosed() {
      this.$refs.editRef.resetFields()
    },
    delDialog(id) {
      this.delTecId = id
      this.delDialogVisible = true
    },
    async delTecById() {
      const { data: res } = await this.$http.delete('tec/del/' + this.delTecId)
      if (res.code !== 20000) {
        return this.$message.error('删除失败!')
      }
      this.getTecList()
      this.delDialogVisible = false
      return this.$message.success('删除成功!')
    }
  },
  mounted() {
    this.getAllDept()
  }
}
</script>

<style lang="less" scoped>
.el-col {
  line-height: 40px;
}

.el-row {
  margin-bottom: 10px;
  text-align: left;
}

.el-form-item {
  text-align: left;
}
</style>
